/*
 * DatabaseAccessException.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.databaseaccess;

import java.text.MessageFormat;

import android.content.Context;
import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;

public class DatabaseAccessException extends Exception {
	private static final long serialVersionUID = 8912429388617000251L;

	private String paramOne;
	private String paramTwo;
	private ErrorCode error;

	public enum ErrorCode {
		ERROR_UNKNOWN, ERROR_CANNOT_FIND_CONTENT_HEADER, ERROR_CANNOT_READ_FILE, ERROR_CANNOT_FIND_RECORD_HEADER,
		ERROR_CANNOT_FIND_DICTIONARY, ERROR_CANNOT_FIND_PREAMBLE_HEADER, ERROR_READING_DICTIONARY, 
		ERROR_CANNOT_FIND_SUPERINDEX_HEADER, ERROR_MATCHING_STRING, ERROR_CANNOT_FIND_INDEXPOSITION_HEADER,
		ERROR_CANNOT_FIND_INDEXRECORD_HEADER
	};
	
	public DatabaseAccessException( Exception e ) {
		super( e.getLocalizedMessage());
	}
	
	public DatabaseAccessException( String msg ) {
		super( msg );
	}
	
	public DatabaseAccessException( ErrorCode error ) {
		this.error = error;
	}

	public DatabaseAccessException(ErrorCode error, String param) {
		setErrorCodeAndParam(error, param);
	}
	
	public DatabaseAccessException(ErrorCode error, String param, String paramTwo) {
		setErrorCodeAndParams(error, param, paramTwo);
	}
	
	public void setErrorCodeAndParam(ErrorCode error, String param) {
		this.paramOne = param;
		this.error = error;
	}

	public void setErrorCodeAndParams(ErrorCode error, String paramOne,
			String paramTwo) {
		this.error = error;
		this.paramOne = paramOne;
		this.paramTwo = paramTwo;
	}

	@Override
	public String getLocalizedMessage() {
		Context c = LearningWordsApplication.get();

		int id = R.string.error_unknown;
		switch (error) {
		case ERROR_CANNOT_FIND_CONTENT_HEADER:
			id = R.string.error_cannot_find_content_header;
			break;
		case ERROR_CANNOT_READ_FILE:
			id = R.string.error_cannot_read_file;
			break;
		case ERROR_CANNOT_FIND_RECORD_HEADER:
			id = R.string.error_cannot_find_record_header;
			break;
		case ERROR_CANNOT_FIND_DICTIONARY:
			id = R.string.error_cannot_find_dictionary;
			break;
		case ERROR_CANNOT_FIND_PREAMBLE_HEADER:
			id = R.string.error_cannot_find_preamble_header;
			break;
		case ERROR_READING_DICTIONARY:
			id = R.string.error_reading_dictionary;
			break;
		case ERROR_CANNOT_FIND_SUPERINDEX_HEADER:
			id = R.string.error_cannot_find_superindex_header;
			break;
		case ERROR_MATCHING_STRING:
			id = R.string.error_matching_string;
			break;
		case ERROR_CANNOT_FIND_INDEXPOSITION_HEADER:
			id = R.string.error_cannot_find_indexposition_header;
			break;
		case ERROR_CANNOT_FIND_INDEXRECORD_HEADER:
			id = R.string.error_cannot_find_indexrecord_header;
			break;
		default:
			id = R.string.error_unknown;
		}

		String msg = c.getResources().getString(id);
		String result = msg;
		if (paramTwo != null && paramOne != null) {
			result = MessageFormat.format(msg, paramOne, paramTwo );
		}
		else if (paramOne != null) {
			result = MessageFormat.format(msg, paramOne);
		} else
			result = msg;
		
		return result;
	}
	
}
